package com.job.meeting.controller;

import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.job.meeting.service.*;
import com.job.meeting.pojo.*;
import com.job.meeting.tools.wechatSystem;

import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/meetingRoom")
public class meetingRoomController {
    @Autowired
    userService us;
    @Autowired
    bookingService bs;
    @Autowired
    meetingRoomService mrs;

    wechatSystem system = null;

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    private class checkBean{
        String id;
        bookingTime time;
    }

    //todo: meetingRoom页面跳转
    @RequestMapping("/page")//TODO:暂未测试
    public String page()
    {
        return "meetingRoom_state1";
    }

    //初始化，根据openid找用户
    @ResponseBody
    @GetMapping("/greeting")
    public user init(@RequestParam("openid") String openid) {
        system = new wechatSystem(us);
        return us.getUserByOpenid(openid);
    }

    //给定一个带有hint功能的会议室json字段，返回该会议室所有的占用情况。
    @ResponseBody
    @PostMapping("/viewMeetingOccupancy")
    public List<booking> viewMeetingOccupancy(@RequestBody String json) {
        meetingRoom room = new Gson().fromJson(json, meetingRoom.class);
        List<booking> bookings = bs.getAllBookingByMeetingRoomID(room.getID());
        Collections.sort(bookings);
        return bookings;
    }

    //获得全部的位置
    @ResponseBody
    @RequestMapping("/getAllLocation")
    public List<location> getAllLocation() {
        return mrs.getAllLocation();
    }

    //根据位置获得全部的会议室信息
    @ResponseBody
    @GetMapping("/getAllMeetingRoomByLocation")
    public List<meetingRoom> getAllMeetingRoomByLocation(@RequestBody String location) {
        List<meetingRoom> meetingRooms = mrs.getAllMeetingRoomByLocation(location);
        Collections.sort(meetingRooms);
        return meetingRooms;
    }

    @ResponseBody
    @GetMapping("/getAllBookingByID")
    //根据会议室id获得全部的已预约
    public List<booking> getAllBookingByID(@RequestBody String id) {
        List<booking> bookings = bs.getAllBookingByMeetingRoomID(Integer.parseInt(id));
        Collections.sort(bookings);
        return bookings;
    }

    //跳转去booking
    @GetMapping("/toBooking")
    public String toBooking(@RequestParam("openid") String openid) {
        return "redirect:/booking?openid=" + openid;
    }
    //跳转去management
    @GetMapping("/toManagement")
    public String toManagement(@RequestParam("openid") String openid) {
        return "redirect:/management?openid=" + openid;
    }
    //跳转去meetingRoom
    @GetMapping("/toMeetingRoom")
    public String toMeetingRoom(@RequestParam("openid") String openid) {
        return "redirect:/meetingRoom?openid=" + openid;
    }
}